package org.electrical.controller.system;

import org.electrical.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Controller
@RequestMapping("/file")
@CrossOrigin(value = "*")
public class FileUpload {

    @RequestMapping("/upload")
    @ResponseBody
    public ResponseResult upload(@RequestParam("file") MultipartFile file) throws Exception {

        if (!file.isEmpty()) {
            String path = "D:\\school\\Y2\\Front\\Graduation_design\\CRM\\electrical\\electrical-webapi\\src\\main\\webapp\\images";
            String fileName = file.getOriginalFilename();
            File filepath = new File(path, fileName);
            file.transferTo(filepath);
        }

        return ResponseResult.createSuccessResult();
    }

}
